<?php

/**
 * @author Josef Fröhle
 * @copyright 2010
 */
    
    #error_reporting(-1);
    #error_reporting(E_ALL);
    ini_set('error_reporting',E_ALL^E_NOTICE); 
    ini_set("display_errors", false);
    ini_set('log_errors',true);
    ini_set('error_log', '../logs/phperror.log');
    
    if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
        header("Content-type: application/xhtml+xml; charset=utf-8");
    } else {
        header("Content-type: text/html; charset=utf-8");
    }
    header("Content-Type: text/html; charset=utf-8");

    //...Stuff before routing occurs
    
    define("PAGE_ENABLE", true);
    
    if (get_magic_quotes_gpc()) {
        function stripslashes_gpc(&$value)
        {
            $value = stripslashes($value);
        }
        array_walk_recursive($_GET, 'stripslashes_gpc');
        array_walk_recursive($_POST, 'stripslashes_gpc');
        array_walk_recursive($_COOKIE, 'stripslashes_gpc');
        array_walk_recursive($_REQUEST, 'stripslashes_gpc');
    } 
    //Set the include path so that the Routing system files can be included.
    # set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/php-router');
    require_once('../config/config.php');
    require_once(CONFIGDIR.DS.'session.php');
    require_once(HELPERDIR.DS.'autoloader_hclass.php');
    $autoloader= new autoloader();
    $autoloader->registerDirectory(CONTROLLERDIR.DS);
    $autoloader->registerDirectory(HELPERDIR.DS);
    
    $mysqli = MyDB::getInstance();    
    
    $_SESSION['userid']=1;
    #if(isset($_SESSION['userid'])) {unset($_SESSION['userid']);}
    
    //Include a PageError class which can be used later. You supply this class.
    require_once('../PageError.php'); 
    require_once('../php-router.php');

    //Create a new instance of Router (you'd likely use a factory or container to
    // manage the instance)
    $router = new Router;

    //Create a new instance of Dispatcher (again, you would probably utilize a
    // factory or container)
    $dispatcher = new Dispatcher;
    $dispatcher->setSuffix('_Controller');
    $dispatcher->setClassPath(CONTROLLERDIR.'/');

    //Set up a 'catch all' default route and add it to the Router.
    //You may want to set up an external file, define your routes there, and
    // and include that file in place of this code block.
    
    require_once(CONFIGDIR.'/route.conf.php');

    //Find the Route for your url
    #$url            = urldecode($_SERVER['REQUEST_URI']);
    $url            = urldecode(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH));
    $found_route    = $router->findRoute($url);
    
    //Dispatch the Route
    if( FALSE === $found_route )
    {
        PageError::show404($url);
    }
    else
    {
        //Note: you would likely use some other http status codes depending
        // on the specific Exception thrown.
        try {
            $dispatcher->dispatch( $found_route );
        } catch ( badClassNameException $e ) {
            PageError::show404($url);
        } catch ( classFileNotFoundException $e ) {
            PageError::show404($url);
        } catch ( classNameNotFoundException $e ) {
            PageError::show404($url);
        } catch ( classMethodNotFoundException $e ) {
            PageError::show404($url);
        } catch ( classNotSpecifiedException $e ) {
            PageError::show404($url);
        } catch ( methodNotSpecifiedException $e ) {
            PageError::show404($url);
        } 
    }


?>